/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.entity.DataType;
import com.thinkgem.jeesite.modules.cms.entity.UserGroup;
import com.thinkgem.jeesite.modules.cms.service.DataTypeService;
import com.thinkgem.jeesite.modules.cms.service.UserGroupService;

/**
 * 用户和员工的对应Controller
 * @author leo
 * @version 2017-02-14
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/userGroup")
public class UserGroupController extends BaseController {

	@Autowired
	private UserGroupService userGroupService;
	@Autowired
	private DataTypeService dataTypeService;
	
	@ModelAttribute
	public UserGroup get(@RequestParam(required=false) String id) {
		UserGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = userGroupService.get(id);
		}
		if (entity == null){
			entity = new UserGroup();
		}
		return entity;
	}
	
	@RequiresPermissions("cms:userGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(UserGroup userGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<UserGroup> page = userGroupService.findPage(new Page<UserGroup>(request, response), userGroup); 
		model.addAttribute("page", page);
		return "modules/cms/userGroupList";
	}

	@RequiresPermissions("cms:userGroup:view")
	@RequestMapping(value = "form")
	public String form(UserGroup userGroup, Model model) {
		List<DataType> datatypes = dataTypeService.getByType("op");
		model.addAttribute("userGroup", userGroup);
		model.addAttribute("datatypes", datatypes);
		return "modules/cms/userGroupForm";
	}

	@RequiresPermissions("cms:userGroup:edit")
	@RequestMapping(value = "save")
	public String save(UserGroup userGroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, userGroup)){
			return form(userGroup, model);
		}
		try {
			userGroupService.save(userGroup);
			addMessage(redirectAttributes, "保存信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			addMessage(redirectAttributes,"保存信息不成功");
		}
		return "redirect:"+Global.getAdminPath()+"/cms/userGroup/?repage";
	}
	
	@RequiresPermissions("cms:userGroup:edit")
	@RequestMapping(value = "delete")
	public String delete(UserGroup userGroup, RedirectAttributes redirectAttributes) {
		userGroupService.delete(userGroup);
		addMessage(redirectAttributes, "删除信息成功");
		return "redirect:"+Global.getAdminPath()+"/cms/userGroup/?repage";
	}

}